package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.dto.CourseTeacherDTO;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.enums.content.CourseAuditEnum;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.common.util.StringUtil;
import com.xuecheng.content.common.constant.ContentErrorCode;
import com.xuecheng.content.convert.CourseTeacherConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.entity.CourseTeacher;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {

    @Autowired
    private CourseBaseService courseBaseService;




    public List<CourseTeacherDTO> queryTeacherList(Long courseId,Long companyId) {

        // 根据课程信息查询是否有权利查询教师信息
        LambdaQueryWrapper<CourseBase> baseQueryWrapper = new LambdaQueryWrapper<>();
        baseQueryWrapper.eq(CourseBase::getId, courseId);
        baseQueryWrapper.eq(CourseBase::getCompanyId, companyId);
        int count = courseBaseService.count(baseQueryWrapper);
        if (count < 1) {
            ExceptionCast.cast(CommonErrorCode.E_100108);
        }

        // 1.构建查询条件
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId, courseId);


        // 2.查询数据
        List<CourseTeacher> teacherList = this.list(queryWrapper);


        // 3.封装结果数据
        List<CourseTeacherDTO> dtos = Collections.emptyList();

        if (!(CollectionUtils.isEmpty(teacherList))) {
            dtos = CourseTeacherConvert.INSTANCE.entitys2dtos(teacherList);
        }

        return dtos;
    }

    @Transactional
    public CourseTeacherDTO createOrModifyTeacher(CourseTeacherDTO dto,Long companyId) {

        // 1.判断关键数据
        if (ObjectUtils.isEmpty(dto.getCourseId())||
                StringUtil.isBlank(dto.getTeacherName())
        ) {
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }


        // 判断课程是否存在
        Long courseId = dto.getCourseId();
        getCourseBaseLogic(companyId, courseId);

        Long courseTeacherId = dto.getCourseTeacherId();

        boolean result = false;


        // 2.判断是否是新增还是修改
        if (ObjectUtils.isEmpty(dto.getCourseTeacherId())) {
            // 添加操作

            CourseTeacher courseTeacher = new CourseTeacher();
            courseTeacher.setCourseId(dto.getCourseId());
            courseTeacher.setIntroduction(dto.getIntroduction());
            courseTeacher.setPhotograph(dto.getPhotograph());
            courseTeacher.setPosition(dto.getPosition());
            courseTeacher.setTeacherName(dto.getTeacherName());

            result = this.save(courseTeacher);

            dto.setCourseTeacherId(courseTeacher.getId());

        } else {
            // 修改操作
            LambdaQueryWrapper<CourseTeacher> teacherQueryWrapper = new LambdaQueryWrapper<>();
            teacherQueryWrapper.eq(CourseTeacher::getId, dto.getCourseTeacherId());
            int countNum = this.count(teacherQueryWrapper);

            if (countNum < 1) {
                ExceptionCast.cast(ContentErrorCode.E_1200503);
            }

            LambdaUpdateWrapper<CourseTeacher> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(CourseTeacher::getTeacherName, dto.getTeacherName());
            updateWrapper.set(CourseTeacher::getIntroduction, dto.getIntroduction());
            updateWrapper.set(CourseTeacher::getPhotograph, dto.getPhotograph());
            updateWrapper.set(CourseTeacher::getPosition, dto.getPosition());

            updateWrapper.eq(CourseTeacher::getId, dto.getCourseTeacherId());

            result = this.update(updateWrapper);
        }

        if (!result) {
            ExceptionCast.cast(ContentErrorCode.E_1200501);
        }

        // 返回数据库最新数据
        CourseTeacher po = this.getById(dto.getCourseTeacherId());

        CourseTeacherDTO resultDTO = CourseTeacherConvert.INSTANCE.entity2dto(po);

        return resultDTO;
    }

    private void getCourseBaseLogic(Long companyId, Long courseId) {
        CourseBaseDTO courseBase = courseBaseService.getCourseBaseById(courseId, companyId);
        String auditStatus = courseBase.getAuditStatus();
        if (CourseAuditEnum.AUDIT_PASTED_STATUS.getCode().equals(auditStatus)||
                CourseAuditEnum.AUDIT_COMMIT_STATUS.getCode().equals(auditStatus)||
                CourseAuditEnum.AUDIT_PUBLISHED_STATUS.getCode().equals(auditStatus)
        ) {
            ExceptionCast.cast(ContentErrorCode.E_120015);
        }
    }

    @Transactional
    public void removeTeacher(Long courseId, Long courseTeacherId, Long companyId){

        // 1.判断关键数据
        if (ObjectUtils.isEmpty(courseId)||
                ObjectUtils.isEmpty(courseTeacherId)||
                ObjectUtils.isEmpty(companyId)
        ) {
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }

        // 2.判断业务数据
        // 课程信息

        getCourseBaseLogic(companyId,courseId);


        // 3.删除教师信息
        boolean result = this.removeById(courseTeacherId);

        if (!result) {
            ExceptionCast.cast(ContentErrorCode.E_1200501);
        }


    }


}
